package com.github.florent37.ohosnosql.sample.slice;


import com.github.florent37.androidnosql.AndroidNoSql;
import com.github.florent37.androidnosql.Listener;
import com.github.florent37.androidnosql.NoSql;
import com.github.florent37.androidnosql.datasaver.LogDataSaver;
import com.github.florent37.ohosnosql.sample.ResourceTable;
import com.github.florent37.ohosnosql.sample.model.Car;
import com.github.florent37.ohosnosql.sample.model.House;
import com.github.florent37.ohosnosql.sample.model.User;
import florent37.github.com.androidnosql.paper.PaperDataSaver;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

import java.util.Arrays;

public class MainAbilitySlice extends AbilitySlice {

    Text tv;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        tv = (Text) findComponentById(ResourceTable.Id_tv);
        AndroidNoSql.initWith(
                new PaperDataSaver(this),
                //new SharedPreferencesDataSaver(getSharedPreferences("test", Context.MODE_PRIVATE)),
                new LogDataSaver("LogDataSaver")
        );
        final NoSql noSql = NoSql.getInstance();

        final User user = new User(
                "flo",
                new House("paris"),
                Arrays.asList(3, 5),
                Arrays.asList(new Car("chevrolet", "camaro"), new Car("ford", "gt"))
        );

        noSql.notify("/user/", new Listener() {
            @Override
            public void nodeChanged(String path, NoSql.Value value) {

            }
        });

        noSql.put("/user/florent/", user);

        final User userFetched = noSql.get("/user/florent/", User.class);
        tv.setText(userFetched.toString());
        System.out.println("nosql log userFetched:" +userFetched);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
